<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>ECharts</title>
    <!-- 引入刚刚下载的 ECharts 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
    <script type="text/javascript"
        src="https://registry.npmmirror.com/echarts/4.9.0/files/dist/echarts.min.js"></script>
    <script type="text/javascript" src="https://registry.npmmirror.com/jquery/3.7.1/files/dist/jquery.min.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
    </style>

    <!--
          🚨🚨非常重要🚨🚨：下面是运行案例需要的第三方脚本，如果是相对链接，下载方法看上面👆👆
          如果你需要本地使用，请下载下来，假设下面代码中有个相对路径资源 /dep/echarts/map/js/china.js，下载方法：https://www.makeapie.cn/dep/echarts/map/js/china.js
      -->
    <!--暂无第三方脚本-->
    <script type="text/javascript" src="https://unpkg.com/echarts-gl@1/dist/echarts-gl.min.js"></script>
</head>

<body>
    <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
    <div id="chart-panel" style="
    width: calc(100vw - 200px);
    height: calc(100vh - 200px);
    padding: 100px;
    margin: 0 auto;
  "></div>
    <script type="text/javascript">
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById("chart-panel"));

        let optionData = [
            {
                name: '相关性预警',
                value: 31,
                proportion: '5.12'
            },
            {
                name: '持续超标',
                value: 70,
                proportion: '11.12'
            },
            {
                name: '限值预警',
                value: 13,
                proportion: '2.15'
            },
            {
                name: '持续恶化',
                value: 481,
                proportion: '79.50'
            },
            {
                name: '监测超标项',
                value: 3,
                proportion: '0.12'
            }
        ];

        function getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h) {
            const midRatio = (startRatio + endRatio) / 2;
            const startRadian = startRatio * Math.PI * 2;
            const endRadian = endRatio * Math.PI * 2;
            const midRadian = midRatio * Math.PI * 2;

            if (startRatio === 0 && endRatio === 1) {
                isSelected = false;
            }

            k = typeof k !== 'undefined' ? k : 1 / 3;
            const offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
            const offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
            const hoverRate = isHovered ? 1.05 : 1;

            return {
                u: {
                    min: -Math.PI,
                    max: Math.PI * 3,
                    step: Math.PI / 32,
                },
                v: {
                    min: 0,
                    max: Math.PI * 2,
                    step: Math.PI / 20,
                },
                x(u, v) {
                    if (u < startRadian) {
                        return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    if (u > endRadian) {
                        return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
                },
                y(u, v) {
                    if (u < startRadian) {
                        return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    if (u > endRadian) {
                        return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
                },
                z(u, v) {
                    if (u < -Math.PI * 0.5) {
                        return Math.sin(u);
                    }
                    if (u > Math.PI * 2.5) {
                        return Math.sin(u) * h * 0.1;
                    }
                    return Math.sin(v) > 0 ? 1 * h * 0.1 : -1;
                },
            };
        }

        function getPie3D(pieData, internalDiameterRatio) {
            const series = [];
            let sumValue = 0;
            let startValue = 0;
            let endValue = 0;
            const legendData = [];
            const k =
                typeof internalDiameterRatio !== 'undefined'
                    ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio)
                    : 1 / 3;

            for (let i = 0; i < pieData.length; i += 1) {
                sumValue += pieData[i].value;

                const seriesItem = {
                    name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name,
                    type: 'surface',
                    parametric: true,
                    wireframe: {
                        show: false,
                    },
                    pieData: pieData[i],
                    pieStatus: {
                        selected: false,
                        hovered: false,
                        k,
                    },
                };

                if (typeof pieData[i].itemStyle !== 'undefined') {
                    const { itemStyle } = pieData[i];
                    typeof pieData[i].itemStyle.color !== 'undefined' ? (seriesItem.itemStyle = { color: pieData[i].itemStyle.color }) : null;
                    typeof pieData[i].itemStyle.opacity !== 'undefined' ? (seriesItem.itemStyle = { opacity: pieData[i].itemStyle.opacity }) : null;
                }

                series.push(seriesItem);
            }

            for (let i = 0; i < series.length; i += 1) {
                endValue = startValue + series[i].pieData.value;
                series[i].pieData.startRatio = startValue / sumValue;
                series[i].pieData.endRatio = endValue / sumValue;
                series[i].parametricEquation = getParametricEquation(
                    series[i].pieData.startRatio,
                    series[i].pieData.endRatio,
                    false,
                    false,
                    k,
                    10 // 在此处传入饼图初始高度h
                );
                startValue = endValue;
                legendData.push(series[i].name);
            }

            const option = {
                title: {
                    text: "预警类型统计",
                    left: "left",
                    textStyle: {
                        fontSize: 14,
                        color: "#333",
                    },
                },
                legend: {
                    orient: "vertical",
                    right: 50,
                    top: "center",
                    formatter: (name) => {
                        const item = optionData.find((item) => item.name === name);
                        if (item) {
                            return `  ${name}：${item.value}个,占比：${item.proportion}%`;
                        }
                        return name;
                    },
                },
                tooltip: {
                    formatter: (params) => {
                        const item = optionData[params.seriesIndex];
                        if (item) {
                            return `${params.marker}${item.name}：${item.value}个，占比：${item.proportion}%`;
                        }
                        return '';
                    },
                },
                xAxis3D: {
                    min: -1,
                    max: 1,
                },
                yAxis3D: {
                    min: -1,
                    max: 1,
                },
                zAxis3D: {
                    min: -1,
                    max: 1,
                },
                grid3D: {
                    show: false,
                    boxHeight: 25, // 修改立体饼图的高度
                    top: '-10%',
                    left: '-10%',
                    viewControl: {
                        alpha: 35, // 饼图翻转的程度
                        beta: 30,
                        rotateSensitivity: 1,
                        zoomSensitivity: 0,
                        panSensitivity: 0,
                        distance: 350, // 距离越小看到的饼图越大
                    },
                },
                series,
            };

            return option;
        }

        const option = getPie3D(optionData, 0); // 可做为调整内环大小 0为实心圆饼图，大于0 小于1 为圆环
        console.log(option, 'option');

        myChart.setOption(option);
    </script>
</body>

</html>